/* eslint-disable */
/**
/**
 * Created by GYL on 2018/7/25
 */
//前置任务
import React, { Component } from 'react';
import MyModal from '../../../components/common/MyModal';
import { Collapse, Icon, Select, Input, Tooltip, Form, Row, Col, TreeSelect, Tag } from 'antd';
import Util from '../../../utils/Util';
import MyIcon from '../../../components/common/MyIcon';
import config from '../../../config';
import moment from 'moment';
import myModalStyles from '../../../components/common/Mymodal.less';
import request from '../../../utils/request';
import MyTable from "../../../components/common/MyTable";
import { message } from 'antd';
const FormItem = Form.Item;
const { Panel } = Collapse;
const { Option } = Select;
const { TextArea } = Input;
const { SHOW_CHILD } = TreeSelect;
const IconFont = Icon.createFromIconfontCN({
    scriptUrl: '//at.alicdn.com/t/font_1315525_kt2bd7ytf.js',
});
class SubTask extends Component {
    componentDidMount() {


        this.setState({
            dataSource: this.props.TaskArr,
        })
    }
    state = {
        columns: [
            { title: '序号', dataIndex: 'num', key: 'num', width: 50 },
            {
                title: '前置关系',
                width: 100,
                render: (text, record, index) => {
                    return (
                        <Select style={{ width: 120 }} defaultValue={record.type} onChange={this.onGX.bind(this, record, index)}>
                            <Option value="">无</Option>
                            <Option value="FS">完成-开始</Option>
                            <Option value="FF">完成-完成</Option>
                            <Option value="SF">开始-完成</Option>
                            <Option value="SS">开始-开始</Option>
                        </Select>
                    )

                }
            },
            {
                title: '子事件名称',
                width: 100,
                render: (text, record, index) => {
                    return (
                        <Select style={{ width: 120 }} defaultValue={record.preposeTaskId} onChange={this.handleChange.bind(this, record, index)}>
                            {

                                this.props.selectArr.map((item, index) => {
                                    return (
                                        <Option value={item.subeventsBasicId} disabled={item.disabled} key={item}>{item.subeventsBasicName}</Option>
                                    )
                                })
                            }
                        </Select>
                    )

                }
            },
            { title: '操作', key: 'operation', width: 100, render: (text, record, index) => <span><a style={{ marginLeft: 5 }} href="javascript:;" onClick={this.DeleteBtn.bind(this, text, record, index)}>删除</a></span> },


        ],
        dataSource: [],
        spinLoading:false
    };


    onGX = (record, index, e) => {
        // console.log(record, index, e)
        this.state.dataSource[index].type = e
        this.setState({
            dataSource: this.state.dataSource
        })
    }

    handleChange = (record, index, e) => {
        // console.log(index, e)
        for (let i = 0; i < this.state.dataSource.length; i++) {
            if (e === this.state.dataSource[i].preposeTaskId) {
                message.warning("同一事件,只能绑定一种关系!")
                // this.state.dataSource.splice(index, 1)
                let arr = this.state.dataSource
                this.setState(
                    {
                        dataSource: [],
                    },
                    () => {
                        //给子确认部门焦点
                        this.setState({
                            dataSource: arr,
                            columns: this.state.columns
                        })
                    },
                );
                return
            }
        }


        this.state.dataSource[index].preposeTaskId = e
        this.setState({
            dataSource: this.state.dataSource
        })
    }
    //确定
    onModalSave = () => {
        this.setState({
            spinLoading:true
        })
        // console.log(this.state.dataSource)
        request({
            url: '/pm-web/projectmanage/preposetaskbasic/insertPreposeTaskBasic',
            method: 'POST',
            data: { ptbList: [...this.state.dataSource], subeventsBasicId: this.props.record.subeventsBasicId }
        }).then(data => {
            // console.log(data)
            if (data.rc === 0) {
                //新增或修改成功
                message.success("修改成功!")
                this.props.onModalSave()
            } else {
                message.warning("修改失败!")
            }

            this.setState({
                spinLoading:false
            })
        })



    }
    //删除
    DeleteBtn = (text, record, index) => {
        // console.log(index, this.state.dataSource)

        this.state.dataSource.splice(index, 1)
        let arr = this.state.dataSource
        this.state.dataSource.forEach((item, index) => {
            item.num = index + 1
        });
        this.setState({
            dataSource: [],
            columns: this.state.columns
        })

        this.setState(
            {
                dataSource: [],
            },
            () => {
                //给子确认部门焦点
                this.setState({
                    dataSource: arr,
                    columns: this.state.columns
                })
            },
        );


    }

    //新增
    onAdd = () => {

        this.state.dataSource.push({
            delayTime: "",
            preposeTaskId: "",
            subeventsBasicId: this.props.record.subeventsBasicId,
            type: "",
            indexID: (new Date()).valueOf() + 'indexID'
        })
        this.state.dataSource.forEach((item, index) => {
            item.num = index + 1
        });
        this.setState({
            dataSource: this.state.dataSource
        })


    }


    render() {
        let t = this;
        // console.log(this.props)
        const {spinLoading} = this.state;
        return (
            <div>
                <MyModal
                    {...t.props}
                    className={myModalStyles.jlModal}
                    destroyOnClose={true}
                    width={800}
                    modalBtnLoading={spinLoading}
                    onModalSave={t.onModalSave}>
                    <div   style={{ height: 300, overflow: 'auto',width:'100%' }}>
                        <Row>
                            {/* <h2 style={{ fontSize: 14, fontWeight: 700 }}>子事件名称:{this.props.record.subeventsBasicName}</h2> */}

                            <span style={{ padding: 3, marginLeft: 15, cursor: 'pointer', color: '#3996FF' }} onClick={this.onAdd}> <IconFont type="icon-xinzeng" />  添加</span>
                        </Row>
                        <MyTable
                            columns={this.state.columns}
                            dataSource={this.state.dataSource}
                            pagination={false}
                        // rowSelection={rowSelection}
                        />
                    </div>


                </MyModal>

            </div>
        )
    }
}
export default Form.create()(SubTask);
